﻿using HandyControl.Controls;
using Prism.Commands;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows;
using WcsZz.Extend;
using wms_wpf.Apiservice;
using wms_wpf.DialogView;
using wms_wpf.Dto;
using wms_wpf.InPut;
using wms_wpf.Views;
using Prism.Mvvm;
using MessageBox = System.Windows.MessageBox;

namespace wms_wpf.ViewModels.ExpressChildViewModel
{
   public class OutofStockChildViewModel : BindableBase, INavigationAware
    {
       
        private readonly IEssService essService;
        string Machine = Environment.GetEnvironmentVariable("Machine", EnvironmentVariableTarget.Machine);
        public OutofStockChildViewModel()
        {
            GetList_View_NeetOutMaterialCommand = new DelegateCommand(GetList_View_NeetOutMaterial);
            DeleteMaterialCommand = new DelegateCommand(DeleteMaterial);
            UpdateNeetQuantityCommand = new DelegateCommand<string>(UpdateNeetQuantity);
            CreateTaskOrderCommand = new DelegateCommand(CreateTaskOrder);
            DeleteNeetMaterialCommand = new DelegateCommand(DeleteNeetOutMaterial);
            UpdateMaterialQuantityCommand = new DelegateCommand(UpdateMaterialQuantity);
            DeleteWasteCommand = new DelegateCommand(DeleteWaste);
            ViewService = new ViewService();
            BaseService = new BaseService();
            CustomService = new CustomService();
            essService = new EssService();
            InPut_ViewService2 = new InPut_ViewService2();
            InPut_BaseService8 = new InPut_BaseService8();
            InPut_BaseService12 = new InPut_BaseService12();
            InPut_BaseService19 = new InPut_BaseService19();
            Inlethousecodes = new List<string>();
            GetList_InlethouesCode();
            InPut_ViewService2.PageSize = 20;
            InPut_ViewService2.PageIndex = 1;
            View_NeetOutMaterialDtos = new List<View_NeetOutMaterialDto>();
            GetList_View_NeetOutMaterial();
            EventAggregatorHelper.EventAggregator.ResgiterMessage(async arg =>
            {

                GetList_View_NeetOutMaterial();

            }, "GetList_View_NeetOutMaterial");
            if (Machine == "2")
            {
                ComboxMachine = 1;
            }
            else
            {
                ComboxMachine = 0;
            }

        }
        public DelegateCommand GetList_View_NeetOutMaterialCommand { get; set; }
        public DelegateCommand DeleteMaterialCommand { get; set; }
        public DelegateCommand<string> UpdateNeetQuantityCommand { get; set; }
        public DelegateCommand CreateTaskOrderCommand { get; set; }
        public DelegateCommand DeleteNeetMaterialCommand { get; set; }
        public DelegateCommand UpdateMaterialQuantityCommand { get; set; }
        public DelegateCommand DeleteWasteCommand { get; set; }
        public IViewService ViewService { get; set; }
        public IBaseService BaseService { get; set; }
        private InPut_BaseService12 inPut_BaseService12;
        public InPut_BaseService12 InPut_BaseService12
        {
            get { return inPut_BaseService12; }
            set { inPut_BaseService12 = value; RaisePropertyChanged(); }
        }
        private InPut_BaseService19 inPut_BaseService19;
        public InPut_BaseService19 InPut_BaseService19
        {
            get { return inPut_BaseService19; }
            set { inPut_BaseService19 = value; RaisePropertyChanged(); }
        }


        private bool isFocus;

        public bool IsFocus
        {
            get { return isFocus; }
            set { isFocus = value;RaisePropertyChanged(); }
        }


        private List<string> inlethousecodes;
        public List<string> Inlethousecodes
        {
            get { return inlethousecodes; }
            set { inlethousecodes = value; RaisePropertyChanged(); }
        }
        public ICustomService CustomService { get; set; }
        private InPut_ViewService2 inPut_ViewService2;
        public InPut_ViewService2 InPut_ViewService2
        {
            get { return inPut_ViewService2; }
            set { inPut_ViewService2 = value; RaisePropertyChanged(); }
        }
        private List<View_NeetOutMaterialDto> view_NeetOutMaterialDtos;
        public List<View_NeetOutMaterialDto> View_NeetOutMaterialDtos
        {
            get { return view_NeetOutMaterialDtos; }
            set { view_NeetOutMaterialDtos = value; RaisePropertyChanged(); }
        }
        private long count;
        public long Count
        {
            get { return count; }
            set { count = value; RaisePropertyChanged(); }
        }
        private long pagecount;
        public long PageCount
        {
            get { return pagecount; }
            set { pagecount = value; RaisePropertyChanged(); }
        }
        private InPut_BaseService8 inPut_BaseService8;
        public InPut_BaseService8 InPut_BaseService8
        {
            get { return inPut_BaseService8; }
            set { inPut_BaseService8 = value; RaisePropertyChanged(); }
        }
        private string boxGridCode;
        public string BoxGridCode
        {
            get { return boxGridCode; }
            set { boxGridCode = value; RaisePropertyChanged(); }
        }
        public int ComboxMachine
        {
            get { return comboxMachine; }
            set { comboxMachine = value; RaisePropertyChanged(); }
        }
        private int comboxMachine;

        private string textBox_text;

        public string TextBox_text
        {
            get { return textBox_text; }
            set { textBox_text = value;RaisePropertyChanged();}
        }
        private View_NeetOutMaterialDto _view_NeetOutMaterialDto;

        public View_NeetOutMaterialDto View_NeetOutMaterialDto
        {
            get { return _view_NeetOutMaterialDto; }
            set { _view_NeetOutMaterialDto = value;RaisePropertyChanged(); }
        }


        public async void GetList_View_NeetOutMaterial()
        {

            EventAggregatorHelper.EventAggregator.SendMessage("", "ClearcountMap");
            if (!string.IsNullOrEmpty(InPut_BaseService8.BoxCode))
            {
                InPut_ViewService2.BoxGridCode = InPut_BaseService8.BoxCode;
            }
            Tuple<List<View_NeetOutMaterialDto>, long?, long?> tuple = await ViewService.GetList_View_NeetOutMaterial(InPut_ViewService2);
            if (tuple == null)
            {
                return;
            }
            View_NeetOutMaterialDtos = tuple.Item1;
            Count = (long)tuple.Item2;
            PageCount = (long)tuple.Item3;
            InPut_ViewService2.BoxGridCode = "";
        }

        List<View_NeetOutMaterialDto> view_NeetOutMaterialDto2s { get; set; } = new List<View_NeetOutMaterialDto>();
        private async void DeleteNeetOutMaterial()
        {

            
            if (View_NeetOutMaterialDto != null)
            {
                await BaseService.DeleteNeetMaterial(View_NeetOutMaterialDto.MaterialCode);
                await BaseService.CreateLog("删除", Jwt.UserName + "删除了" + View_NeetOutMaterialDto.MaterialCode + "需要出的料");
            }
            GetList_View_NeetOutMaterial();
        }

        //取料确认
        private async void DeleteMaterial()
        {
            IsFocus = true;
            try
            {
            

                BoxGridCode = "";
                InPut_BaseService8.JWTUser = Jwt.UserName;
                bool Nullres = InPut_BaseService8.GetType().GetProperties().All(property => property.GetValue(InPut_BaseService8) != null);
                if (!Nullres) { Growl.Info("请填写完整", "msg"); return; }
                BaseDto dto = await BaseService.GetDeleteMaterial(InPut_BaseService8);
                if (dto == null) { return; }
                if (dto != null)
                {
                    BoxGridCode = dto.BoxGridCode;
                }
                if (!string.IsNullOrEmpty(BoxGridCode))
                {
                   
                    CustomInPut3 customInPut3 = new CustomInPut3();
                    customInPut3.source = "ESS";
                    customInPut3.reelId = InPut_BaseService8.MaterialCode;
                    customInPut3.type = "3";
                    customInPut3.partNum = "";
                    if (!string.IsNullOrEmpty(dto.NeetQuantity.ToString()))
                    {
                        customInPut3.qty = (int)dto.NeetQuantity;
                    }
                    customInPut3.moCode = dto.moCode;
                    customInPut3.boxNo = InPut_BaseService8.BoxCode;
                    customInPut3.location = BoxGridCode;
                    customInPut3.hSerial = dto.hSerial;
                    customInPut3.so = "";
                    customInPut3.slotNum = dto.slotNum;
                    customInPut3.feederInfo = "";
                    customInPut3.state = "ok";
                    //mes检查
                    string msg = "ok";
                    if (!string.IsNullOrEmpty(customInPut3.hSerial))
                    {
                        msg = await CustomService.outwarehouseess(customInPut3);

                    }
                    else
                    {
                        msg = await CustomService.outwarehouseessxj(customInPut3);
                    }
                    if (msg == "ok")
                    {
                        await BaseService.DeleteMaterial(InPut_BaseService8);
                        InPut_BaseService8.MaterialCode = "";
                      
                        GetList_View_NeetOutMaterial();
                        //查看需出库储格
                        view_NeetOutMaterialDto2s = await BaseService.GetNeetBoxMaterial(inPut_BaseService8.BoxCode);
                        if (view_NeetOutMaterialDto2s != null && view_NeetOutMaterialDto2s.Count > 0)
                        {
                            DrawBoxWithSeparations(view_NeetOutMaterialDto2s);
                        }
                        else
                        {
                            boxCanvas.Children.Clear();
                        }
                    }
                    else
                    {

                        LognetHelper.WriteLog(msg + customInPut3.reelId);
                        return;

                    }


                }

            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message);
            }



        }
        private async void GetList_InlethouesCode()
        {
            Inlethousecodes = await BaseService.GetList_InlethouesCode("入口");
        }
        //一键上架
        private async void CreateTaskOrder()
        {
            //InPut_BaseService8.BoxCode = await essService.queryNodeReader();
            InPut_BaseService12.BoxCode = InPut_BaseService8.BoxCode;
            if (string.IsNullOrEmpty(InPut_BaseService12.BoxCode))
            {
                Growl.Info("请填写箱号", "msg");
                return;
            }
            string boxcode = await essService.queryNodeReader();
           
                if (string.IsNullOrEmpty(boxcode))
                {
                    Growl.Info("拣选位无料盒,无法上架", "msg"); return;
                }
            

            //生成任务
            if (Machine == "3")
            {
                InPut_BaseService12.InlethouseCode = "LT_CONVEYOR_OUTPUT:POINT:68949:16575";
            }
            string taskorderCode = await BaseService.CreateTaskOrder(InPut_BaseService12);
            if (taskorderCode == null) return;
            View_TaskOrderDto dto = await ViewService.SingleView_TaskOrder(taskorderCode);
            if (dto == null) return;
          
                if (boxcode != dto.BoxCode)
                {
                    Growl.Info("料盒码不一致,无法上架", "msg"); return;
                }
            
            //发送任务给agv
            Ess_Input ess_Input = new Ess_Input();
            ess_Input.taskType = "putaway";
            ess_Input.taskGroupCode = "taskGroupCode-001";
            ess_Input.groupPriority = 0;
            List<Tasks> data = new List<Tasks>();
            Tasks ta = new Tasks();
            ta.taskCode = dto.TaskOrderCode;
            ta.taskPriority = 0;
            data.Add(ta);
            ess_Input.tasks = data;
            TaskDescribe td = new TaskDescribe();
            td.containerCode = dto.BoxCode;
            td.containerType = "CT_KUBOT_STANDARD";
            td.storageTag = "";
            td.fromLocationCode = dto.InlethouseCode;
            td.toLocationCode = dto.WarehouseCode;
            td.toStationCode = "haiport_station_1";
            td.isPutaway = false;
            ta.taskDescribe = td;
            string res = await essService.CreateTask(ess_Input);
            if (res.Contains("OK"))
            {

                await essService.moveContainer(dto.BoxCode);
                InPut_BaseService11 inPut = new InPut_BaseService11();
                inPut.TaskOrderCode = dto.TaskOrderCode;
                inPut.Type = "已下发";
                await BaseService.UpdateTaskOrderState(inPut);
                await BaseService.CreateLog("上架", dto.BoxCode + "执行了上架任务");
                InPut_BaseService8.BoxCode = "";
            
            }
        }
        private async void queryNodeReadermethod()
        {
            InPut_BaseService8.BoxCode = await essService.queryNodeReader();
        }
        private async void UpdateMaterialQuantity()//修改库存数量
        {
            bool Nullres = InPut_BaseService19.GetType().GetProperties().All(property => property.GetValue(InPut_BaseService19) != null);
            if (!Nullres) { Growl.Info("请填写完整", "msg"); return; }
            bool res = await BaseService.UpdateMaterialQuantity(InPut_BaseService19);
            if (res)
            {
                await BaseService.CreateLog("修改", Jwt.UserName + "修改了" + InPut_BaseService19.MaterialCode + "数量：" + InPut_BaseService19.Quantity);
            }
            GetList_View_NeetOutMaterial();
        }
        private async void DeleteWaste()//清除废料
        {
            await BaseService.DeleteWaste();
        }
        //图形
        Canvas boxCanvas = new Canvas();


        private void DrawBoxWithSeparations2()
        {
            boxCanvas.Children.Clear();
            #region 不动线
            Line myLine = new Line();
            myLine.Stroke = System.Windows.Media.Brushes.LightSteelBlue;
            myLine.X1 = 1;
            myLine.X2 = 1;
            myLine.Y1 = 1;
            myLine.Y2 = 400;
            myLine.HorizontalAlignment = HorizontalAlignment.Left;
            myLine.VerticalAlignment = VerticalAlignment.Center;
            myLine.StrokeThickness = 4;
            Line myLine2 = new Line();
            myLine2.Stroke = System.Windows.Media.Brushes.LightSteelBlue;
            myLine2.X1 = 200;
            myLine2.X2 = 200;
            myLine2.Y1 = 1;
            myLine2.Y2 = 400;
            myLine2.HorizontalAlignment = HorizontalAlignment.Left;
            myLine2.VerticalAlignment = VerticalAlignment.Center;
            myLine2.StrokeThickness = 4;
            Line myLine3 = new Line();
            myLine3.Stroke = System.Windows.Media.Brushes.LightSteelBlue;
            myLine3.X1 = 1;
            myLine3.X2 = 200;
            myLine3.Y1 = 1;
            myLine3.Y2 = 1;
            myLine3.HorizontalAlignment = HorizontalAlignment.Left;
            myLine3.VerticalAlignment = VerticalAlignment.Center;
            myLine3.StrokeThickness = 4;
            Line myLine4 = new Line();
            myLine4.Stroke = System.Windows.Media.Brushes.LightSteelBlue;
            myLine4.X1 = 1;
            myLine4.X2 = 200;
            myLine4.Y1 = 400;
            myLine4.Y2 = 400;
            myLine4.HorizontalAlignment = HorizontalAlignment.Left;
            myLine4.VerticalAlignment = VerticalAlignment.Center;
            myLine4.StrokeThickness = 4;
            #endregion
            TextBlock myTextBox = new TextBlock();
            myTextBox.Text = "asasasas";
            myTextBox.IsEnabled = false;
            myTextBox.Width = 100;
            myTextBox.Height = 30;
            Canvas.SetLeft(myTextBox, 65); // 设置左侧位置
            Canvas.SetTop(myTextBox, 20); // 设置顶部位置
            Image myImage = new System.Windows.Controls.Image();
            myImage.Source = new BitmapImage(new Uri("/Images/电子元器件.png", UriKind.RelativeOrAbsolute));
            myImage.Height = 90;
            myImage.Visibility = Visibility.Visible;
            Canvas.SetLeft(myImage, 60);
            Canvas.SetTop(myImage, 35);
            boxCanvas.Children.Add(myTextBox);
            boxCanvas.Children.Add(myImage);

            TextBlock myTextBox2 = new TextBlock();
            myTextBox2.Text = "asasasas";
            myTextBox2.IsEnabled = false;
            myTextBox2.Width = 100;
            myTextBox2.Height = 30;
            Canvas.SetLeft(myTextBox2, 65); // 设置左侧位置
            Canvas.SetTop(myTextBox2, 153); // 设置顶部位置
            Image myImage2 = new System.Windows.Controls.Image();
            myImage2.Source = new BitmapImage(new Uri("/Images/电子元器件.png", UriKind.RelativeOrAbsolute));
            myImage2.Height = 90;
            myImage2.Visibility = Visibility.Visible;
            Canvas.SetLeft(myImage2, 60);
            Canvas.SetTop(myImage2, 168);
            boxCanvas.Children.Add(myTextBox2);
            boxCanvas.Children.Add(myImage2);

            TextBlock myTextBox3 = new TextBlock();
            myTextBox3.Text = "asasasas";
            myTextBox3.IsEnabled = false;
            myTextBox3.Width = 100;
            myTextBox3.Height = 30;
            Canvas.SetLeft(myTextBox3, 65); // 设置左侧位置
            Canvas.SetTop(myTextBox3, 286); // 设置顶部位置
            Image myImage3 = new System.Windows.Controls.Image();
            myImage3.Source = new BitmapImage(new Uri("/Images/电子元器件.png", UriKind.RelativeOrAbsolute));
            myImage3.Height = 90;
            myImage3.Visibility = Visibility.Visible;
            Canvas.SetLeft(myImage3, 60);
            Canvas.SetTop(myImage3, 301);
            boxCanvas.Children.Add(myTextBox3);
            boxCanvas.Children.Add(myImage3);


            #region 3格线
            Line myLine33 = new Line();
            myLine33.Stroke = System.Windows.Media.Brushes.LightSteelBlue;
            myLine33.X1 = 1;
            myLine33.X2 = 200;
            myLine33.Y1 = 133;
            myLine33.Y2 = 133;
            myLine33.HorizontalAlignment = HorizontalAlignment.Left;
            myLine33.VerticalAlignment = VerticalAlignment.Center;
            myLine33.StrokeThickness = 4;

            Line myLine34 = new Line();
            myLine34.Stroke = System.Windows.Media.Brushes.LightSteelBlue;
            myLine34.X1 = 1;
            myLine34.X2 = 200;
            myLine34.Y1 = 266;
            myLine34.Y2 = 266;
            myLine34.HorizontalAlignment = HorizontalAlignment.Left;
            myLine34.VerticalAlignment = VerticalAlignment.Center;
            myLine34.StrokeThickness = 4;
            #endregion

            TextBlock myTextBox33 = new TextBlock();
            myTextBox33.Text = "1";
            myTextBox33.IsEnabled = false;
            myTextBox33.Width = 100;
            myTextBox33.Height = 30;
            Canvas.SetLeft(myTextBox33, 100); // 设置左侧位置
            Canvas.SetTop(myTextBox33, 10); // 设置顶部位置

            TextBlock myTextBox34 = new TextBlock();
            myTextBox34.Text = "2";
            myTextBox34.IsEnabled = false;
            myTextBox34.Width = 100;
            myTextBox34.Height = 30;
            Canvas.SetLeft(myTextBox34, 100); // 设置左侧位置
            Canvas.SetTop(myTextBox34, 143); // 设置顶部位置

            TextBlock myTextBox35 = new TextBlock();
            myTextBox35.Text = "3";
            myTextBox35.IsEnabled = false;
            myTextBox35.Width = 100;
            myTextBox35.Height = 30;
            Canvas.SetLeft(myTextBox35, 100); // 设置左侧位置
            Canvas.SetTop(myTextBox35, 273); // 设置顶部位置

            // 将 TextBox 添加到画布中
            boxCanvas.Children.Add(myLine);
            boxCanvas.Children.Add(myLine2);
            boxCanvas.Children.Add(myLine3);
            boxCanvas.Children.Add(myLine4);
            boxCanvas.Children.Add(myLine33);
            boxCanvas.Children.Add(myLine34);
            boxCanvas.Children.Add(myTextBox34);
            boxCanvas.Children.Add(myTextBox33);
            boxCanvas.Children.Add(myTextBox35);

        }
        private void DrawBoxWithSeparations(List<View_NeetOutMaterialDto> parameter)
        {
            boxCanvas.Children.Clear();
            #region 不动线
            Line myLine = new Line();
            myLine.Stroke = System.Windows.Media.Brushes.LightSteelBlue;
            myLine.X1 = 1;
            myLine.X2 = 1;
            myLine.Y1 = 1;
            myLine.Y2 = 400;
            myLine.HorizontalAlignment = HorizontalAlignment.Left;
            myLine.VerticalAlignment = VerticalAlignment.Center;
            myLine.StrokeThickness = 4;
            Line myLine2 = new Line();
            myLine2.Stroke = System.Windows.Media.Brushes.LightSteelBlue;
            myLine2.X1 = 200;
            myLine2.X2 = 200;
            myLine2.Y1 = 1;
            myLine2.Y2 = 400;
            myLine2.HorizontalAlignment = HorizontalAlignment.Left;
            myLine2.VerticalAlignment = VerticalAlignment.Center;
            myLine2.StrokeThickness = 4;
            Line myLine3 = new Line();
            myLine3.Stroke = System.Windows.Media.Brushes.LightSteelBlue;
            myLine3.X1 = 1;
            myLine3.X2 = 200;
            myLine3.Y1 = 1;
            myLine3.Y2 = 1;
            myLine3.HorizontalAlignment = HorizontalAlignment.Left;
            myLine3.VerticalAlignment = VerticalAlignment.Center;
            myLine3.StrokeThickness = 4;
            Line myLine4 = new Line();
            myLine4.Stroke = System.Windows.Media.Brushes.LightSteelBlue;
            myLine4.X1 = 1;
            myLine4.X2 = 200;
            myLine4.Y1 = 400;
            myLine4.Y2 = 400;
            myLine4.HorizontalAlignment = HorizontalAlignment.Left;
            myLine4.VerticalAlignment = VerticalAlignment.Center;
            myLine4.StrokeThickness = 4;
            #endregion
            #region 2格线
            Line myLine5 = new Line();
            myLine5.Stroke = System.Windows.Media.Brushes.LightSteelBlue;
            myLine5.X1 = 1;
            myLine5.X2 = 200;
            myLine5.Y1 = 200;
            myLine5.Y2 = 200;
            myLine5.HorizontalAlignment = HorizontalAlignment.Left;
            myLine5.VerticalAlignment = VerticalAlignment.Center;
            myLine5.StrokeThickness = 4;

            if (parameter[0].BoxGridQuantity == 2)
            {
                foreach (var item in parameter)
                {

                    if (item.BoxGridCode.Substring(item.BoxGridCode.Length - 1) == "1")
                    {
                        //1#位置
                        TextBlock myTextBox = new TextBlock();
                        myTextBox.Text = item.MaterialName;
                        myTextBox.IsEnabled = false;
                        myTextBox.Width = 100;
                        myTextBox.Height = 30;
                        Canvas.SetLeft(myTextBox, 60); // 设置左侧位置
                        Canvas.SetTop(myTextBox, 20); // 设置顶部位置
                        Image myImage = new System.Windows.Controls.Image();
                        myImage.Source = new BitmapImage(new Uri("/Images/电子元器件.png", UriKind.RelativeOrAbsolute));
                        myImage.Height = 100;
                        myImage.Visibility = Visibility.Visible;
                        Canvas.SetLeft(myImage, 50);
                        Canvas.SetTop(myImage, 55);
                        boxCanvas.Children.Add(myTextBox);
                        boxCanvas.Children.Add(myImage);

                    }
                    else if (item.BoxGridCode.Substring(item.BoxGridCode.Length - 1) == "2")
                    {
                        //2#位置
                        TextBlock myTextBox2 = new TextBlock();
                        myTextBox2.Text = item.MaterialName;
                        myTextBox2.IsEnabled = false;
                        myTextBox2.Height = 30;
                        Canvas.SetLeft(myTextBox2, 60); // 设置左侧位置
                        Canvas.SetTop(myTextBox2, 220); // 设置顶部位置
                        boxCanvas.Children.Add(myTextBox2);
                        Image myImage2 = new Image();
                        myImage2.Source = new BitmapImage(new Uri("/Images/电子元器件.png", UriKind.RelativeOrAbsolute));
                        myImage2.Width = 100;
                        myImage2.Height = 100;
                        myImage2.Visibility = Visibility.Visible;
                        Canvas.SetLeft(myImage2, 50);
                        Canvas.SetTop(myImage2, 255);
                        boxCanvas.Children.Add(myImage2);

                    }
                }
            }

            //序号
            TextBlock myTextBox3 = new TextBlock();
            myTextBox3.Text = "1";
            myTextBox3.IsEnabled = false;
            myTextBox3.Width = 100;
            myTextBox3.Height = 30;
            Canvas.SetLeft(myTextBox3, 10); // 设置左侧位置
            Canvas.SetTop(myTextBox3, 10); // 设置顶部位置

            TextBlock myTextBox4 = new TextBlock();
            myTextBox4.Text = "2";
            myTextBox4.IsEnabled = false;
            myTextBox4.Width = 100;
            myTextBox4.Height = 30;
            Canvas.SetLeft(myTextBox4, 10); // 设置左侧位置
            Canvas.SetTop(myTextBox4, 210); // 设置顶部位置


            #endregion
            #region 3格线
            Line myLine33 = new Line();
            myLine33.Stroke = System.Windows.Media.Brushes.LightSteelBlue;
            myLine33.X1 = 1;
            myLine33.X2 = 200;
            myLine33.Y1 = 133;
            myLine33.Y2 = 133;
            myLine33.HorizontalAlignment = HorizontalAlignment.Left;
            myLine33.VerticalAlignment = VerticalAlignment.Center;
            myLine33.StrokeThickness = 4;

            Line myLine34 = new Line();
            myLine34.Stroke = System.Windows.Media.Brushes.LightSteelBlue;
            myLine34.X1 = 1;
            myLine34.X2 = 200;
            myLine34.Y1 = 266;
            myLine34.Y2 = 266;
            myLine34.HorizontalAlignment = HorizontalAlignment.Left;
            myLine34.VerticalAlignment = VerticalAlignment.Center;
            myLine34.StrokeThickness = 4;
            if (parameter[0].BoxGridQuantity == 3)
            {
                foreach (var item in parameter)
                {

                    if (item.BoxGridCode.Substring(item.BoxGridCode.Length - 1) == "1")
                    {
                        //1#位置
                        TextBlock myTextBox = new TextBlock();
                        myTextBox.Text = item.MaterialName;
                        myTextBox.IsEnabled = false;
                        myTextBox.Width = 100;
                        myTextBox.Height = 30;
                        Canvas.SetLeft(myTextBox, 65); // 设置左侧位置
                        Canvas.SetTop(myTextBox, 20); // 设置顶部位置
                        Image myImage = new System.Windows.Controls.Image();
                        myImage.Source = new BitmapImage(new Uri("/Images/电子元器件.png", UriKind.RelativeOrAbsolute));
                        myImage.Height = 90;
                        myImage.Visibility = Visibility.Visible;
                        Canvas.SetLeft(myImage, 60);
                        Canvas.SetTop(myImage, 35);
                        boxCanvas.Children.Add(myTextBox);
                        boxCanvas.Children.Add(myImage);

                    }
                    else if (item.BoxGridCode.Substring(item.BoxGridCode.Length - 1) == "2")
                    {
                        //2#位置
                        TextBlock myTextBox2 = new TextBlock();
                        myTextBox2.Text = item.MaterialName;
                        myTextBox2.IsEnabled = false;
                        myTextBox2.Width = 100;
                        myTextBox2.Height = 30;
                        Canvas.SetLeft(myTextBox2, 65); // 设置左侧位置
                        Canvas.SetTop(myTextBox2, 153); // 设置顶部位置
                        Image myImage2 = new System.Windows.Controls.Image();
                        myImage2.Source = new BitmapImage(new Uri("/Images/电子元器件.png", UriKind.RelativeOrAbsolute));
                        myImage2.Height = 90;
                        myImage2.Visibility = Visibility.Visible;
                        Canvas.SetLeft(myImage2, 60);
                        Canvas.SetTop(myImage2, 168);
                        boxCanvas.Children.Add(myTextBox2);
                        boxCanvas.Children.Add(myImage2);

                    }
                    else if (item.BoxGridCode.Substring(item.BoxGridCode.Length - 1) == "3")
                    {
                        //2#位置
                        TextBlock myTextBox03 = new TextBlock();
                        myTextBox03.Text = item.MaterialName;
                        myTextBox03.IsEnabled = false;
                        myTextBox03.Width = 100;
                        myTextBox03.Height = 30;
                        Canvas.SetLeft(myTextBox03, 65); // 设置左侧位置
                        Canvas.SetTop(myTextBox03, 286); // 设置顶部位置
                        Image myImage3 = new System.Windows.Controls.Image();
                        myImage3.Source = new BitmapImage(new Uri("/Images/电子元器件.png", UriKind.RelativeOrAbsolute));
                        myImage3.Height = 90;
                        myImage3.Visibility = Visibility.Visible;
                        Canvas.SetLeft(myImage3, 60);
                        Canvas.SetTop(myImage3, 301);
                        boxCanvas.Children.Add(myTextBox03);
                        boxCanvas.Children.Add(myImage3);

                    }
                }
            }

            //序号
            TextBlock myTextBox33 = new TextBlock();
            myTextBox33.Text = "1";
            myTextBox33.IsEnabled = false;
            myTextBox33.Width = 100;
            myTextBox33.Height = 30;
            Canvas.SetLeft(myTextBox33, 100); // 设置左侧位置
            Canvas.SetTop(myTextBox33, 10); // 设置顶部位置

            TextBlock myTextBox34 = new TextBlock();
            myTextBox34.Text = "2";
            myTextBox34.IsEnabled = false;
            myTextBox34.Width = 100;
            myTextBox34.Height = 30;
            Canvas.SetLeft(myTextBox34, 100); // 设置左侧位置
            Canvas.SetTop(myTextBox34, 143); // 设置顶部位置

            TextBlock myTextBox35 = new TextBlock();
            myTextBox35.Text = "3";
            myTextBox35.IsEnabled = false;
            myTextBox35.Width = 100;
            myTextBox35.Height = 30;
            Canvas.SetLeft(myTextBox35, 100); // 设置左侧位置
            Canvas.SetTop(myTextBox35, 273); // 设置顶部位置
            #endregion
            #region  4格线
            Line myLine6 = new Line();
            myLine6.Stroke = System.Windows.Media.Brushes.LightSteelBlue;
            myLine6.X1 = 100;
            myLine6.X2 = 100;
            myLine6.Y1 = 0;
            myLine6.Y2 = 400;
            myLine6.HorizontalAlignment = HorizontalAlignment.Left;
            myLine6.VerticalAlignment = VerticalAlignment.Center;
            myLine6.StrokeThickness = 4;
            if (parameter[0].BoxGridQuantity == 4)
            {
                foreach (var item in parameter)
                {
                    if (item.BoxGridCode.Substring(item.BoxGridCode.Length - 1) == "1")
                    {
                        //1#
                        TextBlock myTextBox41 = new TextBlock();
                        myTextBox41.Text = item.MaterialName;
                        myTextBox41.IsEnabled = false;
                        myTextBox41.Width = 100;
                        myTextBox41.Height = 30;
                        Canvas.SetLeft(myTextBox41, 10); // 设置左侧位置
                        Canvas.SetTop(myTextBox41, 25); // 设置顶部位置

                        Image myImage41 = new Image();
                        myImage41.Source = new BitmapImage(new Uri("/Images/电子元器件.png", UriKind.RelativeOrAbsolute));
                        myImage41.Height = 80;
                        myImage41.Visibility = Visibility.Visible;
                        Canvas.SetLeft(myImage41, 11);
                        Canvas.SetTop(myImage41, 60);

                        boxCanvas.Children.Add(myTextBox41);
                        boxCanvas.Children.Add(myImage41);



                    }
                    else if (item.BoxGridCode.Substring(item.BoxGridCode.Length - 1) == "2")
                    {
                        //2#
                        TextBlock myTextBox42 = new TextBlock();
                        myTextBox42.Text = item.MaterialName;
                        myTextBox42.IsEnabled = false;
                        myTextBox42.Width = 100;
                        myTextBox42.Height = 30;
                        Canvas.SetLeft(myTextBox42, 110); // 设置左侧位置
                        Canvas.SetTop(myTextBox42, 25); // 设置顶部位置

                        Image myImage42 = new Image();
                        myImage42.Source = new BitmapImage(new Uri("/Images/电子元器件.png", UriKind.RelativeOrAbsolute));
                        myImage42.Height = 80;
                        myImage42.Visibility = Visibility.Visible;
                        Canvas.SetLeft(myImage42, 111);
                        Canvas.SetTop(myImage42, 60);
                        boxCanvas.Children.Add(myTextBox42);
                        boxCanvas.Children.Add(myImage42);


                    }
                    else if (item.BoxGridCode.Substring(item.BoxGridCode.Length - 1) == "3")
                    {
                        //3#
                        TextBlock myTextBox43 = new TextBlock();
                        myTextBox43.Text = item.MaterialName;
                        myTextBox43.IsEnabled = false;
                        myTextBox43.Width = 100;
                        myTextBox43.Height = 30;
                        Canvas.SetLeft(myTextBox43, 10); // 设置左侧位置
                        Canvas.SetTop(myTextBox43, 225); // 设置顶部位置

                        Image myImage43 = new Image();
                        myImage43.Source = new BitmapImage(new Uri("/Images/电子元器件.png", UriKind.RelativeOrAbsolute));
                        myImage43.Height = 80;
                        myImage43.Visibility = Visibility.Visible;
                        Canvas.SetLeft(myImage43, 11);
                        Canvas.SetTop(myImage43, 260);

                        boxCanvas.Children.Add(myTextBox43);
                        boxCanvas.Children.Add(myImage43);


                    }
                    else if (item.BoxGridCode.Substring(item.BoxGridCode.Length - 1) == "4")
                    {
                        //4#
                        TextBlock myTextBox44 = new TextBlock();
                        myTextBox44.Text = item.MaterialName;
                        myTextBox44.IsEnabled = false;
                        myTextBox44.Width = 100;
                        myTextBox44.Height = 30;
                        Canvas.SetLeft(myTextBox44, 110); // 设置左侧位置
                        Canvas.SetTop(myTextBox44, 225); // 设置顶部位置

                        Image myImage44 = new Image();
                        myImage44.Source = new BitmapImage(new Uri("/Images/电子元器件.png", UriKind.RelativeOrAbsolute));
                        myImage44.Height = 80;
                        myImage44.Visibility = Visibility.Visible;
                        Canvas.SetLeft(myImage44, 111);
                        Canvas.SetTop(myImage44, 260);
                        boxCanvas.Children.Add(myTextBox44);
                        boxCanvas.Children.Add(myImage44);
                    }
                }
            }





            //序号
            TextBlock myTextBox45 = new TextBlock();
            myTextBox45.Text = "1";
            myTextBox45.IsEnabled = false;
            myTextBox45.Width = 100;
            myTextBox45.Height = 30;
            Canvas.SetLeft(myTextBox45, 50); // 设置左侧位置
            Canvas.SetTop(myTextBox45, 5); // 设置顶部位置

            TextBlock myTextBox46 = new TextBlock();
            myTextBox46.Text = "2";
            myTextBox46.IsEnabled = false;
            myTextBox46.Width = 100;
            myTextBox46.Height = 30;
            Canvas.SetLeft(myTextBox46, 150); // 设置左侧位置
            Canvas.SetTop(myTextBox46, 5); // 设置顶部位置

            TextBlock myTextBox47 = new TextBlock();
            myTextBox47.Text = "3";
            myTextBox47.IsEnabled = false;
            myTextBox47.Width = 100;
            myTextBox47.Height = 30;
            Canvas.SetLeft(myTextBox47, 50); // 设置左侧位置
            Canvas.SetTop(myTextBox47, 205); // 设置顶部位置

            TextBlock myTextBox48 = new TextBlock();
            myTextBox48.Text = "4";
            myTextBox48.IsEnabled = false;
            myTextBox48.Width = 100;
            myTextBox48.Height = 30;
            Canvas.SetLeft(myTextBox48, 150); // 设置左侧位置
            Canvas.SetTop(myTextBox48, 205); // 设置顶部位置                           







            #endregion
            #region  6格线
            Line myLine7 = new Line();
            myLine7.Stroke = System.Windows.Media.Brushes.LightSteelBlue;
            myLine7.X1 = 1;
            myLine7.X2 = 200;
            myLine7.Y1 = 133;
            myLine7.Y2 = 133;
            myLine7.HorizontalAlignment = HorizontalAlignment.Left;
            myLine7.VerticalAlignment = VerticalAlignment.Center;
            myLine7.StrokeThickness = 4;
            Line myLine8 = new Line();
            myLine8.Stroke = System.Windows.Media.Brushes.LightSteelBlue;
            myLine8.X1 = 1;
            myLine8.X2 = 200;
            myLine8.Y1 = 266;
            myLine8.Y2 = 266;
            myLine8.HorizontalAlignment = HorizontalAlignment.Left;
            myLine8.VerticalAlignment = VerticalAlignment.Center;
            myLine8.StrokeThickness = 4;
            //标题

            if (parameter[0].BoxGridQuantity == 6)
            {
                foreach (var item in parameter)
                {

                    if (item.BoxGridCode.Substring(item.BoxGridCode.Length - 1) == "1")
                    {
                        //1#
                        TextBlock myTextBox61 = new TextBlock();
                        myTextBox61.Text = item.MaterialName;
                        myTextBox61.IsEnabled = false;
                        myTextBox61.Width = 100;
                        myTextBox61.Height = 30;
                        Canvas.SetLeft(myTextBox61, 10); // 设置左侧位置
                        Canvas.SetTop(myTextBox61, 25); // 设置顶部位置

                        Image myImage61 = new Image();
                        myImage61.Source = new BitmapImage(new Uri("/Images/电子元器件.png", UriKind.RelativeOrAbsolute));
                        myImage61.Height = 70;
                        myImage61.Visibility = Visibility.Visible;
                        Canvas.SetLeft(myImage61, 15);
                        Canvas.SetTop(myImage61, 45);
                        boxCanvas.Children.Add(myTextBox61);
                        boxCanvas.Children.Add(myImage61);


                    }
                    else if (item.BoxGridCode.Substring(item.BoxGridCode.Length - 1) == "2")
                    {
                        //2#
                        TextBlock myTextBox62 = new TextBlock();
                        myTextBox62.Text = item.MaterialName;
                        myTextBox62.IsEnabled = false;
                        myTextBox62.Width = 100;
                        myTextBox62.Height = 30;
                        Canvas.SetLeft(myTextBox62, 110); // 设置左侧位置
                        Canvas.SetTop(myTextBox62, 25); // 设置顶部位置

                        Image myImage62 = new Image();
                        myImage62.Source = new BitmapImage(new Uri("/Images/电子元器件.png", UriKind.RelativeOrAbsolute));
                        myImage62.Height = 70;
                        myImage62.Visibility = Visibility.Visible;
                        Canvas.SetLeft(myImage62, 115);
                        Canvas.SetTop(myImage62, 45);
                        boxCanvas.Children.Add(myTextBox62);
                        boxCanvas.Children.Add(myImage62);


                    }
                    else if (item.BoxGridCode.Substring(item.BoxGridCode.Length - 1) == "3")
                    {
                        //3#
                        TextBlock myTextBox63 = new TextBlock();
                        myTextBox63.Text = item.MaterialName;
                        myTextBox63.IsEnabled = false;
                        myTextBox63.Width = 100;
                        myTextBox63.Height = 30;
                        Canvas.SetLeft(myTextBox63, 10); // 设置左侧位置
                        Canvas.SetTop(myTextBox63, 158); // 设置顶部位置

                        Image myImage63 = new Image();
                        myImage63.Source = new BitmapImage(new Uri("/Images/电子元器件.png", UriKind.RelativeOrAbsolute));
                        myImage63.Height = 70;
                        myImage63.Visibility = Visibility.Visible;
                        Canvas.SetLeft(myImage63, 15);
                        Canvas.SetTop(myImage63, 175);
                        boxCanvas.Children.Add(myTextBox63);
                        boxCanvas.Children.Add(myImage63);


                    }
                    else if (item.BoxGridCode.Substring(item.BoxGridCode.Length - 1) == "4")
                    {
                        //4#
                        TextBlock myTextBox64 = new TextBlock();
                        myTextBox64.Text = item.MaterialName;
                        myTextBox64.IsEnabled = false;
                        myTextBox64.Width = 100;
                        myTextBox64.Height = 30;
                        Canvas.SetLeft(myTextBox64, 110); // 设置左侧位置
                        Canvas.SetTop(myTextBox64, 158); // 设置顶部位置

                        Image myImage64 = new Image();
                        myImage64.Source = new BitmapImage(new Uri("/Images/电子元器件.png", UriKind.RelativeOrAbsolute));
                        myImage64.Height = 70;
                        myImage64.Visibility = Visibility.Visible;
                        Canvas.SetLeft(myImage64, 115);
                        Canvas.SetTop(myImage64, 175);
                        boxCanvas.Children.Add(myTextBox64);
                        boxCanvas.Children.Add(myImage64);


                    }
                    else if (item.BoxGridCode.Substring(item.BoxGridCode.Length - 1) == "5")
                    {
                        //5#
                        TextBlock myTextBox65 = new TextBlock();
                        myTextBox65.Text = item.MaterialName;
                        myTextBox65.IsEnabled = false;
                        myTextBox65.Width = 100;
                        myTextBox65.Height = 30;
                        Canvas.SetLeft(myTextBox65, 10); // 设置左侧位置
                        Canvas.SetTop(myTextBox65, 291); // 设置顶部位置

                        Image myImage65 = new Image();
                        myImage65.Source = new BitmapImage(new Uri("/Images/电子元器件.png", UriKind.RelativeOrAbsolute));
                        myImage65.Height = 70;
                        myImage65.Visibility = Visibility.Visible;
                        Canvas.SetLeft(myImage65, 15);
                        Canvas.SetTop(myImage65, 308);
                        boxCanvas.Children.Add(myTextBox65);
                        boxCanvas.Children.Add(myImage65);


                    }
                    else if (item.BoxGridCode.Substring(item.BoxGridCode.Length - 1) == "6")
                    {

                        //6#
                        TextBlock myTextBox66 = new TextBlock();
                        myTextBox66.Text = item.MaterialName;
                        myTextBox66.IsEnabled = false;
                        myTextBox66.Width = 100;
                        myTextBox66.Height = 30;
                        Canvas.SetLeft(myTextBox66, 110); // 设置左侧位置
                        Canvas.SetTop(myTextBox66, 291); // 设置顶部位置

                        Image myImage66 = new Image();
                        myImage66.Source = new BitmapImage(new Uri("/Images/电子元器件.png", UriKind.RelativeOrAbsolute));
                        myImage66.Height = 70;
                        myImage66.Visibility = Visibility.Visible;
                        Canvas.SetLeft(myImage66, 115);
                        Canvas.SetTop(myImage66, 308);
                        boxCanvas.Children.Add(myTextBox66);
                        boxCanvas.Children.Add(myImage66);
                    }
                }
            }
            //序号
            TextBlock myTextBox67 = new TextBlock();
            myTextBox67.Text = "1";
            myTextBox67.IsEnabled = false;
            myTextBox67.Width = 100;
            myTextBox67.Height = 30;
            Canvas.SetLeft(myTextBox67, 50);
            Canvas.SetTop(myTextBox67, 5);

            TextBlock myTextBox68 = new TextBlock();
            myTextBox68.Text = "2";
            myTextBox68.IsEnabled = false;
            myTextBox68.Width = 100;
            myTextBox68.Height = 30;
            Canvas.SetLeft(myTextBox68, 150);
            Canvas.SetTop(myTextBox68, 5);

            TextBlock myTextBox69 = new TextBlock();
            myTextBox69.Text = "3";
            myTextBox69.IsEnabled = false;
            myTextBox69.Width = 100;
            myTextBox69.Height = 30;
            Canvas.SetLeft(myTextBox69, 50);
            Canvas.SetTop(myTextBox69, 138);

            TextBlock myTextBox70 = new TextBlock();
            myTextBox70.Text = "4";
            myTextBox70.IsEnabled = false;
            myTextBox70.Width = 100;
            myTextBox70.Height = 30;
            Canvas.SetLeft(myTextBox70, 150);
            Canvas.SetTop(myTextBox70, 138);

            TextBlock myTextBox71 = new TextBlock();
            myTextBox71.Text = "5";
            myTextBox71.IsEnabled = false;
            myTextBox71.Width = 100;
            myTextBox71.Height = 30;
            Canvas.SetLeft(myTextBox71, 50);
            Canvas.SetTop(myTextBox71, 271);

            TextBlock myTextBox72 = new TextBlock();
            myTextBox72.Text = "6";
            myTextBox72.IsEnabled = false;
            myTextBox72.Width = 100;
            myTextBox72.Height = 30;
            Canvas.SetLeft(myTextBox72, 150);
            Canvas.SetTop(myTextBox72, 271);
            #endregion
            // 将 TextBox 添加到画布中
            boxCanvas.Children.Add(myLine);
            boxCanvas.Children.Add(myLine2);
            boxCanvas.Children.Add(myLine3);
            boxCanvas.Children.Add(myLine4);
            boxCanvas.Children.Add(myLine33);
            if (parameter[0].BoxGridQuantity == 2)
            {
                boxCanvas.Children.Add(myLine5);
                boxCanvas.Children.Add(myTextBox3);
                boxCanvas.Children.Add(myTextBox4);

            }
            else if (parameter[0].BoxGridQuantity == 3)
            {
                boxCanvas.Children.Add(myLine33);
                boxCanvas.Children.Add(myLine34);
                boxCanvas.Children.Add(myTextBox34);
                boxCanvas.Children.Add(myTextBox33);
                boxCanvas.Children.Add(myTextBox35);
            }
            else if (parameter[0].BoxGridQuantity == 4)
            {
                boxCanvas.Children.Add(myLine5);
                boxCanvas.Children.Add(myLine6);

                boxCanvas.Children.Add(myTextBox45);
                boxCanvas.Children.Add(myTextBox46);
                boxCanvas.Children.Add(myTextBox47);
                boxCanvas.Children.Add(myTextBox48);

            }
            else if (parameter[0].BoxGridQuantity == 6)
            {
                boxCanvas.Children.Add(myLine6);
                boxCanvas.Children.Add(myLine7);
                boxCanvas.Children.Add(myLine8);

                boxCanvas.Children.Add(myTextBox67);
                boxCanvas.Children.Add(myTextBox68);
                boxCanvas.Children.Add(myTextBox69);
                boxCanvas.Children.Add(myTextBox70);
                boxCanvas.Children.Add(myTextBox71);
                boxCanvas.Children.Add(myTextBox72);

            }
     
        }
        private async void UpdateNeetQuantity(string obj)//修改需要出库数量
        {
            try
            {
                var dialogResult = HandyControl.Controls.Dialog.Show(new Dialog9(obj));
                GetList_View_NeetOutMaterial();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                LognetHelper.WriteLog("系统-UpdateNeetQuantity", ex);
            }

        }
        public void OnNavigatedTo(NavigationContext navigationContext)
        {

        }
        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return false;
        }
        public void OnNavigatedFrom(NavigationContext navigationContext)
        {

        }
    }
}
